package com.jy.oms.framework.thread;

import com.alibaba.fastjson.JSON;
import com.jy.oms.framework.util.SpringUtil;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * controller
 *
 * @author liuquan
 */
@WebServlet("/thread/status")
public class ThreadPoolController extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String action = request.getParameter("action");
        if (StringUtils.isBlank(action)) {
            print(request, response, ThreadPoolResponse.failure("参数[action]不能为空"));
            return;
        }

        ThreadPoolManager threadPoolManager = SpringUtil.getBean(ThreadPoolManager.class);
        if (StringUtils.equalsIgnoreCase(action, "status")) {
            //查看所有线程池状态
            Map<String, ThreadPoolStatus> threadPoolStatusMap = threadPoolManager.getThreadPoolStatus();
            List<ThreadPoolStatus> threadPoolStatusList = threadPoolStatusMap.values().stream().collect(Collectors.toList());
            print(request, response, ThreadPoolResponse.success(threadPoolStatusList));
        } else if (StringUtils.equalsIgnoreCase(action, "singleStatus")) {
            //查看指定线程池状态
            String threadPoolKey = request.getParameter("threadPoolKey");
            if (StringUtils.isBlank(threadPoolKey)) {
                print(request, response, ThreadPoolResponse.failure("参数[threadPoolKey]不能为空"));
                return;
            }

            Map<String, ThreadPoolStatus> threadPoolStatusMap = threadPoolManager.getThreadPoolStatus();
            ThreadPoolStatus threadPoolStatus = threadPoolStatusMap.get(threadPoolKey);
            print(request, response, ThreadPoolResponse.success(Collections.singletonList(threadPoolStatus)));
        } else if (StringUtils.equalsIgnoreCase(action, "configureThreadPool")) {
            //动态修改线程池参数
            String threadPoolKey = request.getParameter("threadPoolKey");
            String corePoolSize = request.getParameter("corePoolSize");
            String maximumPoolSize = request.getParameter("maximumPoolSize");
            if (StringUtils.isAnyBlank(threadPoolKey, corePoolSize, maximumPoolSize)) {
                print(request, response, ThreadPoolResponse.failure("参数[threadPoolKey、corePoolSize、maximumPoolSize]不能为空"));
                return;
            }

            int corePoolSizeInt = Integer.parseInt(corePoolSize);
            int maximumPoolSizeInt = Integer.parseInt(maximumPoolSize);
            if (corePoolSizeInt > maximumPoolSizeInt) {
                print(request, response, ThreadPoolResponse.failure("核心线程池数[corePoolSize]必须小于等于最大线程数[maximumPoolSize]"));
                return;
            }

            threadPoolManager.configureThreadPool(threadPoolKey, corePoolSizeInt, maximumPoolSizeInt);
            Map<String, ThreadPoolStatus> threadPoolStatusMap = threadPoolManager.getThreadPoolStatus();
            ThreadPoolStatus threadPoolStatus = threadPoolStatusMap.get(threadPoolKey);
            print(request, response, ThreadPoolResponse.success(Collections.singletonList(threadPoolStatus)));
        } else {
            print(request, response, ThreadPoolResponse.failure("不支持的参数[action]"));
        }
    }

    private void print(HttpServletRequest request, HttpServletResponse response, Object msg) throws IOException {
        if(this.isAjaxRequest(request)){
            response.setContentType("application/json");
        }else{
            response.setContentType("text/html");
        }
        response.setCharacterEncoding("utf-8");
        PrintWriter writer = response.getWriter();
        writer.write(JSON.toJSONString(msg));
        writer.flush();
        writer.close();
    }

    private boolean isAjaxRequest(HttpServletRequest request) {
        String accept = request.getHeader("accept");
        String xRequestedWith = request.getHeader("X-Requested-With");
        return accept != null && accept.indexOf("application/json") != -1
                || xRequestedWith != null && xRequestedWith.indexOf("XMLHttpRequest") != -1;
    }

}
